package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditUserInfoActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_btn_right)
    Button topBtnRight;
    @BindView(R.id.edt_input_name)
    EditText edtInputName;
    @BindView(R.id.top_iv_right)
    ImageView topIvRight;
    private String title;
    private String input_info;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_user_info;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        edtInputName.setFocusable(true);
        edtInputName.setFocusableInTouchMode(true);
        edtInputName.requestFocus();
        title = getIntent().getStringExtra("TITlE_NAME");
        input_info = getIntent().getStringExtra("input_info");
        topTitle.setText(title);
        if (input_info.contains("请输入")) {
            edtInputName.setHint(input_info);
        } else {
            edtInputName.setText(input_info);
        }

        if (title.equals("昵称")) {
            edtInputName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
        } else if (title.equals("真实姓名")) {
            edtInputName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
        } else {
            edtInputName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
        }
        topBtnRight.setText("保存");
    }

    @OnClick({R.id.top_btn_left, R.id.top_btn_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.top_btn_right:
                saveInfo();
                break;
        }
    }

    private void saveInfo() {
        String string = edtInputName.getText().toString().trim();
        if (!title.equals("个性签名")) {
            if (TextUtils.isEmpty(string)) {
                ToastUtils.showShort(input_info);
                return;
            }
        }
        JSONObject object = new JSONObject();
        try {
            if (title.equals("昵称")) {
                object.put("nickname", string);
            } else if (title.equals("真实姓名")) {
                object.put("realName", string);
            } else {
                object.put("introduce", string);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        showDialog();
        new UserAPI().onSavePerson(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
                Intent intent = new Intent();
                if (title.equals("昵称")) {
                    intent.putExtra("USER_NAME", edtInputName.getText().toString().trim());
                } else if (title.equals("真实姓名")) {
                    intent.putExtra("REAL_NAME", edtInputName.getText().toString().trim());
                } else {
                    intent.putExtra("USER_SIGNATURE", edtInputName.getText().toString().trim());
                }
                setResult(99, intent);
                finish();
            }
        });
    }
}